<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>商品报表</title>
    <base href="http://localhost/campus/">
    <link rel="shortcut icon" href="static/favicon.ico" type="image/x-icon"/>
    <script src="static/js/jquery.min.js"></script>
    <script src="echarts/echarts.min.js"></script>
</head>
<body>
<div id="newProduct" style="width:43%;height:90%;padding: 1% 1.5%;
    margin: 1% 1.5%;background: #fff;
    border-radius: 8px;float: left"></div>
<div id="categoryProduct" style="width:43%;height:90%;padding: 1% 1.5%;
    margin: 1% 1.5%;background: #fff;
    border-radius: 8px;float: left"></div>
<script type="text/javascript">
    // 基于准备好的dom，初始化echarts实例
    var newProductChart = echarts.init(document.getElementById('newProduct'));
    var categoryProductChart = echarts.init(document.getElementById('categoryProduct'));

    let timeData = null;
    let newProductData = null;
    let curCategoryProductData = null;
    let categoryData = null;

    $.ajax({
        url: "http://localhost/campus/reports/product/new"
        , type: "GET"
        , async: false
        , success: function (res) {
            timeData = res.data.time;
            newProductData = res.data.newProductList;
        }
    });

    $.ajax({
        url: "http://localhost/campus/reports/product/category"
        , type: "GET"
        , async: false
        , success: function (res) {
            curCategoryProductData = res.data.curCategoryProductCountList;
            categoryData = res.data.categoryList;
        }
    });

    // 指定图表的配置项和数据
    var newProductOption = {
        title: {
            text: '商品每日新增曲线图'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['新增商品数']
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: timeData
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                formatter: '{value}'
            }
        },
        series: [
            {
                name: '新增商品数',
                type: 'line',
                data: newProductData,
                markPoint: {
                    data: [
                        {type: 'max', name: '最大值'},
                        {type: 'min', name: '最小值'}
                    ]
                },
                markLine: {
                    data: [
                        {type: 'average', name: '平均值'}
                    ]
                }
            }
        ]
    };

    let len = curCategoryProductData.length;
    let category = [];
    for (let i = 0; i < len; i++) {
        let tmp = {};
        tmp['name'] = categoryData[i];
        tmp['value'] = curCategoryProductData[i];
        category[i] = tmp;
    }

    var categoryProductOption = {
        title: {
            text: '商品分类占比',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left',
        },
        series: [
            {
                name: '商品分类名',
                type: 'pie',
                radius: '50%',
                data: category,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };

    // 使用刚指定的配置项和数据显示图表。
    newProductChart.setOption(newProductOption);
    categoryProductChart.setOption(categoryProductOption);
</script>
</body>
</html>
